package math.komplex.impl;
import math.komplex.*;
public class SimpleKomplex implements Komplex
{
	private double minbq = 1.0E-300;
	private double re = 0;
	private double im = 0;
	public SimpleKomplex()
	{
		
	}
	public SimpleKomplex(double re,double im)
	{
		this.re = re;
		this.im = im;
	}
	@Override
	public double getRe() 
	{
		return re;
	}
	@Override
	public double getIm() 
	{
		return im;
	}
	@Override
	public double getBetrag() 
	{
		double d = re * re + im * im;
		double erg = Math.sqrt(d);
		return erg;
	}
	@Override
	public void setRe(double d) 
	{
		re = d;		
	}
	@Override
	public void setIm(double d) 
	{
		im = d;		
	}
	@Override
	public Komplex add(Komplex z1, Komplex z2) 
	{
		SimpleKomplex erg = new SimpleKomplex();
		erg.re = z1.getRe() + z2.getRe();
		erg.im = z1.getIm() + z2.getIm();
		return erg;
	}
	@Override
	public Komplex mult(Komplex z1, Komplex z2) 
	{
		SimpleKomplex erg = new SimpleKomplex();
		erg.re = z1.getRe() * z2.getRe() - z1.getIm() * z2.getIm();
		erg.im = z1.getRe() * z2.getIm() + z1.getIm() * z2.getRe();
		return erg;
	}
	@Override
	public Komplex mult(double d, Komplex z) 
	{
		SimpleKomplex erg = new SimpleKomplex();
		erg.re = d * z.getRe();
		erg.im = d * z.getIm();
		return erg;
	}
	@Override
	public Komplex getInvers(Komplex z) throws Exception 
	{
		SimpleKomplex erg = new SimpleKomplex();
		double x = z.getRe();
		double y = z.getIm();
		double bq = x * x + y * y;
		if (bq > minbq)
		{
			erg.re = x / bq;
			erg.im = 0 - (y / bq);
		}
		else
		{
			throw new Exception("SimpleKomplex:getInvers:Betrag ist zu klein");
		}
		return erg;
	}
	@Override
	public String toString()
	{
		String erg = "(" + re + "," + im + ")";
		return erg;
	}
	@Override
	public Komplex copy()
	{
		SimpleKomplex erg = new SimpleKomplex(re,im);
		return erg;
	}
}
